<?php
/**
 * Created by PhpStorm.
 * User: luzy
 * Date: 2018/4/20
 * Time: 下午12:04
 */

class Wenjuanitem extends Base_Controller
{
    public function __construct()
    {
        $this->_page_size = 10;
        $this->_form_type = 'wenjuan_item';
        $this->_list_where = array();
        $this->_action_list = array('EDIT');//EDIT,DEL,array('controller'=>'admin/commanswer/','function'=>'showlist','label'=>'查看回答')
        $this->_controller = 'admin/wenjuanitem/';//admin/user/
        parent::__construct();
    }

    public function showList($qid = 0,$page = 1)
    {
        $this->_external_id=$qid;
        $this->_list_where['qid']=$qid;

        $limit = $this->_page_size;
        $offset = ($page-1) * $limit;
        $pdata = $this->getPagination($this->_controller.'showList/'.$this->_external_id.'/');

        $res = $this->getList($offset,$limit);

        $this->load->model('wenjuan_model');
        //filter list data
        foreach ($res['list'] as &$row){
            $row['qid'] = $this->wenjuan_model->getQuestion($qid);
        }//处理表数据

        //有创建按钮
        $createbtn = $this->load->view('admin/form/createbtn',array('controller'=>site_url($this->_controller),'param'=>$this->_external_id),true);
        //有返回按钮
        $backbtn = $this->load->view('admin/form/backbtn',array('controller'=>site_url('admin/wenjuanq/')),true);

        $data = array('headers'=>$res['headers'],'list'=>$res['list'],'pagination'=>$pdata['pagination'],'createbtn'=>$createbtn,'backbtn'=>$backbtn);
        $content = $this->load->view('admin/form/list',$data,true);

        echo $this->getAdminView($content);
    }

    public function update($id = 0,$qid = 0)
    {
        $this->_external_id = $qid;

        $post = $this->input->post();
        if(empty($post)){
            $form = $this->updateBuildForm($id);
            $content = $this->load->view('admin/form/edit',array('controller'=>site_url($this->_controller),'param'=>$this->_external_id,'form'=>$form),true);
            echo $this->getAdminView($content);
        }else{
            /******待编辑******/
            //filter post data
            $post['qid']=$qid;
            $post['key']=trim($post['key']);
            $post['item']=trim($post['item']);
            $post['content']=trim($post['content']);
            $post['color']=trim($post['color']);
            $post['sort']=(int)$post['sort'];

            $this->_update_data = $post;
            parent::update($id);
        }
    }
    public function afterupdate($pkey,$pkeyval){}

    public function create($qid = 0)
    {
        $this->_external_id=$qid;
        $post = $this->input->post();
        if(empty($post)){
            $form = $this->createBuildForm();
            $content = $this->load->view('admin/form/edit',array('controller'=>site_url($this->_controller),'param'=>$this->_external_id,'form'=>$form),true);
            echo $this->getAdminView($content);
        }else{
            /******待编辑******/
            //filter post data
            $post['qid']=$qid;
            $post['key']=trim($post['key']);
            $post['item']=trim($post['item']);
            $post['content']=trim($post['content']);
            $post['color']=trim($post['color']);
            $post['sort']=(int)$post['sort'];

            $this->_create_data = $post;
            parent::create();
        }
    }

    public function aftercreate($pkeyval){return;}

    public function delete($id = 0,$externalid = 0){}

    public function afterdelete($pkey,$pkeyval){}

}